package com.ruoyi.house.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.house.domain.TbHouse;
import com.ruoyi.house.domain.TbHouseProperties;
import com.ruoyi.house.domain.vo.TbHouseVo;
import com.ruoyi.house.mapper.TbApartmentMapper;
import com.ruoyi.house.mapper.TbHouseMapper;
import com.ruoyi.house.service.ITbHousePropertiesService;
import com.ruoyi.house.service.ITbHouseService;

/**
 * 房屋管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-08
 */
@Service
public class TbHouseServiceImpl implements ITbHouseService 
{
    @Autowired
    private TbHouseMapper tbHouseMapper;

    @Autowired
    private TbApartmentMapper tbApartmentMapper;

    @Autowired
    private ITbHousePropertiesService tbHousePropertiesService;

    /**
     * 查询房屋管理
     * 
     * @param houseId 房屋管理主键
     * @return 房屋管理
     */
    @Override
    public TbHouse selectTbHouseByHouseId(Long houseId)
    {
        return tbHouseMapper.selectTbHouseByHouseId(houseId);
    }

    /**
     * 查询房屋管理列表
     * 
     * @param tbHouse 房屋管理
     * @return 房屋管理
     */
    @Override
    public List<TbHouse> selectTbHouseList(TbHouse tbHouse)
    {
        return tbHouseMapper.selectTbHouseList(tbHouse);
    }

    /**
     * 新增房屋管理
     * 
     * @param tbHouse 房屋管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbHouse(TbHouse tbHouse)
    {
        int rows = tbHouseMapper.insertTbHouse(tbHouse);
        tbApartmentMapper.updateRoomCount(tbHouse.getApartmentId());
        
        // 创建默认的房屋属性
        TbHouseProperties properties = new TbHouseProperties();
        properties.setHouseId(tbHouse.getHouseId());
        properties.setFloorSpace("未知"); // 设置默认值
        properties.setTypeHouse("未知"); // 设置默认值
        properties.setTowards("未知"); // 设置默认值
        properties.setFacility("无"); // 设置默认值
        tbHousePropertiesService.insertTbHouseProperties(properties);
        
        return rows;
    }

    /**
     * 修改房屋管理
     * 
     * @param tbHouse 房屋管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTbHouse(TbHouse tbHouse)
    {
        int rows = tbHouseMapper.updateTbHouse(tbHouse);
        tbApartmentMapper.updateRoomCount(tbHouse.getApartmentId());
        return rows;
    }

    /**
     * 批量删除房屋管理
     * 
     * @param houseIds 需要删除的房屋管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTbHouseByHouseIds(Long[] houseIds)
    {
        // 获取所有需要更新的公寓ID
        Set<Long> apartmentIds = Arrays.stream(houseIds)
            .map(houseId -> tbHouseMapper.selectTbHouseByHouseId(houseId).getApartmentId())
            .collect(Collectors.toSet());

        int rows = tbHouseMapper.deleteTbHouseByHouseIds(houseIds);

        // 删除相关的房屋属性
        tbHousePropertiesService.deleteTbHousePropertiesByHouseIds(houseIds);

        // 更新所有相关公寓的房间数量
        for (Long apartmentId : apartmentIds) {
            tbApartmentMapper.updateRoomCount(apartmentId);
        }

        return rows;
    }

    /**
     * 删除房屋管理信息
     * 
     * @param houseId 房屋管理主键
     * @return 结果
     */
    @Override
    public int deleteTbHouseByHouseId(Long houseId)
    {
        return tbHouseMapper.deleteTbHouseByHouseId(houseId);
    }

    /**
     * 查询房屋管理列表(包含公寓名称)
     * 
     * @param tbHouse 房屋管理
     * @return 房屋管理
     */
    @Override
    public List<TbHouseVo> selectTbHouseVoList(TbHouse tbHouse)
    {
        return tbHouseMapper.selectTbHouseVoList(tbHouse);
    }

    @Override
    public TbHouse selectTbHouseByApartmentNameAndDoorplate(String apartmentNameAndDoorplate) {
        String[] parts = apartmentNameAndDoorplate.split("\\s+", 2);
        if (parts.length == 2) {
            String apartmentName = parts[0];
            String doorplate = parts[1];
            return tbHouseMapper.selectTbHouseByApartmentNameAndDoorplate(apartmentName, doorplate);
        }
        return null;
    }

}
